package com.kakavr.emp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.emp.service.IEmployeeService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.IShopAdminService;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;
/**
 * 店员管理
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("empAdmin")
public class EmployeeAdminController extends BaseController {

	@Resource
	private IShopAdminService shopAdminService;
	@Resource
	private IEmployeeService employeeService;

	/**
	 * 添加店员
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request) {
		String mobile = request.getParameter("mobile");
		if (StringUtils.isBlank(mobile)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "联系方式");
		}
		String employee = request.getParameter("employee");
		if (StringUtils.isBlank(employee)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "姓名");
		}
		String username = request.getParameter("userName");
		if (StringUtils.isBlank(username)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "用户名");
		}
		String password = request.getParameter("password");
		if (StringUtils.isBlank(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "密码");
		}
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "密码");
		}
		String status = request.getParameter("status");
		if (StringUtils.isBlank(status)) {
			status = "1";
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("status", status);
		map.put("mobile", mobile);
		map.put("employee", employee);
		map.put("empPass", EncryptionUtils.md5(password));
		map.put("username", username);
		map.put("isMaster", 1);
		map.put("createTime", DateTime.now().toDate());
		map.put("storeId", SessionUtils.getShopId());
		shopAdminService.addEmployee(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改店员
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request) {
		String empId = request.getParameter("empId");
		if (!ValidatorUtils.isNumber(empId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店员");
		}
		String mobile = request.getParameter("mobile");
		if (StringUtils.isBlank(mobile)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "联系方式");
		}
		String employee = request.getParameter("employee");
		if (StringUtils.isBlank(employee)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "姓名");
		}
		String status = request.getParameter("status");
		if (StringUtils.isBlank(status)) {
			status = "1";
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("status", status);
		map.put("mobile", mobile);
		map.put("employee", employee);
		map.put("empId", empId);
		shopAdminService.updateEmployee(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改员工密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatePass", method = RequestMethod.POST)
	public Map<String, Object> updatePass(HttpServletRequest request) {
		String empId = request.getParameter("empId");
		if (!ValidatorUtils.isNumber(empId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "店员");
		}
		String password = request.getParameter("password");
		if (StringUtils.isBlank(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "密码");
		}
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "密码");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("empPass", EncryptionUtils.md5(password));
		map.put("empId", empId);
		map.put("isUpPass", 1);
		shopAdminService.updateEmployee(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改员工状态
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	public Map<String, Object> updateStatus(HttpServletRequest request) {
		String empId = request.getParameter("empId");
		if (StringUtils.isBlank(empId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店员");
		}
		String status = request.getParameter("status");
		if (StringUtils.isBlank(status)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店员状态");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("status", status);
		map.put("empId", empId);
		shopAdminService.updateEmployee(map);
		return MsgResult.okResult();
	}

	/**
	 * 店员列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectList(HttpServletRequest request) {
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("storeId", SessionUtils.getShopId());
		return MsgResult.okResultData(employeeService.selectEmpByPage(map));
	}
	
	/**
	 * 删除员工
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public Map<String, Object> delete(HttpServletRequest request){
		String empId = request.getParameter("empId");
		if (StringUtils.isBlank(empId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "员工");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("empId", empId);
		map.put("storeId", SessionUtils.getShopId());
		employeeService.deleteEmp(map);
		return MsgResult.okResult();
	}

}
